package com.bootdo.system.controller;

import com.bootdo.system.config.MD5Utils;
import com.bootdo.system.domain.ApptupianDO;
import com.bootdo.system.domain.FrontuserDO;
import com.bootdo.system.service.ApptupianService;
import com.bootdo.system.service.FrontuserService;
import com.bootdo.system.utils.HttpUtils;
import com.bootdo.system.utils.R;
import com.bootdo.system.utils.RandomValidateCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
@Controller
public class LoginController {


    @Autowired
    private FrontuserService frontuserService;
    @Autowired
    private ApptupianService apptupianService;

    @GetMapping("/login")
    public String loginPage(Model model) {

        ApptupianDO loginpic = apptupianService.get(1);
        model.addAttribute("loginpic",loginpic);

        return "login";
    }

    @PostMapping("/login")
    String Login(String username, String password, String verify, HttpServletRequest request, HttpServletResponse response) {
        log.info("username=" + username + " password = " + password + "  code = " + verify);
        try {
            //从session中获取随机数
            String random = (String) request.getSession().getAttribute(RandomValidateCodeUtil.RANDOMCODEKEY);
            if (StringUtils.isBlank(verify)) {
                request.setAttribute("logintip", "请输入验证码");
                return "login";
            }
            if (random.equals(verify)) {
            } else {
                request.setAttribute("logintip", "请输入正确的验证码");
                return "login";
            }
        } catch (Exception e) {
            request.setAttribute("logintip", "验证码校验失败");
            return "login";
        }
        password = MD5Utils.encrypt(username, password);

        try {
           FrontuserDO frontuserDO =  frontuserService.login(username, password);
            if (frontuserDO != null) {
                request.getSession().setAttribute("user", frontuserDO);
                //添加cookie
                HttpUtils.AddCookie(password, response);

                return "redirect:/index";
            } else {
                request.setAttribute("logintip", "用户或密码错误");
                return "login";
            }

        } catch (AuthenticationException e) {
            request.setAttribute("logintip", "用户或密码错误");
            return "login";
        }
    }

    @GetMapping("/logout")
    String logout(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        //清除session
        session.invalidate();
        //清除cookie
        HttpUtils.DelCookie(request, response);
        return "redirect:/login";
    }


    /**
     * 生成验证码
     */
    @GetMapping(value = "/getVerify")
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
            response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
            randomValidateCode.getRandcode(request, response);//输出验证码图片方法
        } catch (Exception e) {
            log.error("获取验证码失败 ", e);
        }
    }
}
